package com.huajx.tuopuda.client.udp.codec;

import com.huajx.tuopuda.client.udp.TuopudaDirect;
import com.huajx.tuopuda.client.udp.TuopudaUtil;
import com.huajx.util.date.DateTime;
import com.huajx.util.date.DateUtil;

import java.util.Date;

/**
 * @program: di-parent
 * @description:
 * @author: lisa 叶新华
 * @create: 2019-09-28 05:05
 */
public class SetDatetime extends AbstractCodec {
    private final static int function = TuopudaDirect.D_1095;

    public SetDatetime(int sn) {
        setSn(sn);
    }

    public static SetDatetime getInstance(int sn) {
        return new SetDatetime(sn);
    }

    @Override
    public byte[] encode() {
        return encode(getSn(), function, (bytes) -> {
            Date date = DateUtil.date();
            int year = DateUtil.year(date) - 2000;
            int month = DateUtil.month(date) + 1;
            int day = DateUtil.dayOfMonth(date);
            int hour = DateUtil.hour(date, true);
            int second = DateUtil.second(date);
            int minute = DateUtil.minute(date);

            int week = DateUtil.dayOfWeek(date) - 1;

            if (week == 0) {
                week = 7;
            }

            int checkSum = 0;

            System.out.println(DateUtil.format(date, "YYMMDDHHmmss"));
            System.out.println(DateTime.now().dayOfWeek());

            checkSum += TuopudaUtil.replaceBytes(bytes, year, 5, 1);
            checkSum += TuopudaUtil.replaceBytes(bytes, month, 6, 1);
            checkSum += TuopudaUtil.replaceBytes(bytes, day, 7, 1);
            checkSum += TuopudaUtil.replaceBytes(bytes, week, 8, 1);
            checkSum += TuopudaUtil.replaceBytes(bytes, hour, 9, 1);
            checkSum += TuopudaUtil.replaceBytes(bytes, minute, 10, 1);
            checkSum += TuopudaUtil.replaceBytes(bytes, second, 11, 1);

            return checkSum;
        });
    }
}
